<?php
include "connect/connect_dsccmis.php";
$Submit=$_POST["Submit"];
if($Submit <> ""){
 //GET variable
 $chkbox_prov = $_POST["chkbox_prov"];
 $chkbox_cause = $_POST["chkbox_cause"];
 $date_start = $_POST["date_start"];
 $date_end = $_POST["date_end"];
 
 $arr_prov_selected = array("90"=>0,"94"=>0,"95"=>0,"96"=>0);
 $arr_cause_selected = array("1"=>0,"2"=>0,"3"=>0,"4"=>0,"5"=>0,"9"=>0);
 $arr_check = array("","checked");
 //เงื่อนไขการเลือกจังหวัด
 $cond_prov = "";
 foreach($chkbox_prov as $prov){
   $arr_prov_selected[$prov]=1;
   $cond_prov = $cond_prov." ".$prov."";
 }
 $cond_prov_str = "";
 if($cond_prov!=""){//มีการเลือกจังหวัด ให้สร้างเงื่อนไข
  	$cond_prov = trim($cond_prov);
  	$cond_prov = "(".$cond_prov.")";
  	$cond_prov = str_replace(' ',',',$cond_prov);
  	//echo $cond_prov;
	$cond_prov_str = $cond_prov;
 }
 //echo $cond_prov_str;
 
 //เงื่อนไขการเลือกสาเหตุ
 $cond_cause = "";
 foreach($chkbox_cause as $cause){
   if($cause=="3 4 5 9"){
   	$arr_cause_selected["3"]=1;
   }else{
   	$arr_cause_selected[$cause]=1;
   }
   $cond_cause = $cond_cause." ".$cause."";
 }
 $cond_cause_str = "";
 if($cond_prov!=""){//มีการเลือกจังหวัด ให้สร้างเงื่อนไข
  	$cond_cause = trim($cond_cause);
  	$cond_cause = "(".$cond_cause.")";
  	$cond_cause = str_replace(' ',',',$cond_cause);
  	//echo $cond_cause;
	$cond_cause_str = $cond_cause;
 }
 //echo $cond_cause_str; 
}
?>
<script src="common/calendar_selector.js" type="text/javascript"></script>
<style type="text/css">
<!--
.style2 {color: green}
-->
</style>

<form name="form_qevent" action="index.php?tab=victim&tab2=1" method="post">
<table>
<tr><td><table border="0" cellspacing="0">
  <tr>
    <td width="109" height="30">จังหวัดที่เกิดเหตุ</td>
    <td height="30" colspan="4"><input name="chkbox_prov[]" type="checkbox" id="chkbox_prov" value="90"<?=$arr_check[$arr_prov_selected["90"]]?>/>
สงขลา
  <input name="chkbox_prov[]" type="checkbox" id="chkbox_prov" value="94"<?=$arr_check[$arr_prov_selected["94"]]?>/>
ปัตตานี
<input name="chkbox_prov[]" type="checkbox" id="chkbox_prov" value="95"<?=$arr_check[$arr_prov_selected["95"]]?>/>
ยะลา
<input name="chkbox_prov[]" type="checkbox" id="chkbox_prov" value="96"<?=$arr_check[$arr_prov_selected["96"]]?>/>
นราธิวาส</td>
    </tr>
  <tr>
    <td height="30">ช่วงเวลา</td>
    <td width="73" height="30">ตั้งแต่</td>
    <td width="157" height="30">วันที่
      <input name="date_start" type="text" id="date_start" value="<?php if($date_start <> "") echo $date_start; else echo "2547-01-04";?>" size="10" maxlength="10" />
      <a href="#" onclick="return getCalendar(document.forms[0].date_start);"><img src="images/calendar.png" border="0" /></a></td>
    <td width="20" height="30">ถึง</td>
    <td width="326" height="30">
      วันที่
      <input name="date_end" type="text" id="date_end" value="<?php if($date_end <> "") echo $date_end; else echo getDateTimeNow("-m-d");?>" size="10" maxlength="10" />
      <a href="#" onclick="return getCalendar(document.forms[0].date_end);"><img src="images/calendar.png" border="0" /></a></td>
  </tr>
  <tr>
    <td height="30">สาเหตุ</td>
    <td height="30" colspan="4"><input name="chkbox_cause[]" type="checkbox" id="chkbox_cause" value="1"<?=$arr_check[$arr_cause_selected["1"]]?>/>
ความไม่สงบ
  <input name="chkbox_cause[]" type="checkbox" id="chkbox_cause" value="2"<?=$arr_check[$arr_cause_selected["2"]]?>/>
อยู่ระหว่างตรวจสอบ
<input name="chkbox_cause[]" type="checkbox" id="chkbox_cause" value="3 4 5 9"<?=$arr_check[$arr_cause_selected["3"]]?>/>
อุบัติเหต +เรื่องส่วนตัว+อื่นๆ+ไม่ระบุ</td>
    </tr>
  <tr>
    <td height="50" colspan="5" align="center"><input type="submit" name="Submit" value="สอบถาม" />
      <input type="submit" name="Submit2" value="ยกเลิก" /></td>
    </tr>
</table>
    </td>
</tr>
<?php
if($Submit <> ""){
$sql = "select max(perupddte) as upddte from mis_viewlosofper";
$rs=mysql_query($sql);
$row=mysql_fetch_array($rs);
$last_update=getDateTimeDMY($row["upddte"]);
?>
<tr>
  <td height="40"><div align="center"><span class="style2">(ปรับปรุงข้อมูลล่าสุด <?=$last_update?>)</span></div></td>
</tr>
<?php
}
?>
<tr>
<td><div align="center"><img src="gph_victim_losbody_sex.php?cond_prov_str=<?=$cond_prov_str?>&cond_cause_str=<?=$cond_cause_str?>&date_start=<?=$date_start?>&date_end=<?=$date_end?>"/></div></td>
</tr>
<tr>
  <td>
    <div align="center">
      <?php
if($Submit <> ""){
$cond_prov_str = " and prov in ".$cond_prov_str; 
$cond_cause_str = " and cdscc in ".$cond_cause_str; 

//สร้าเงื่อนไข วันทีเริ่มต้น
if($date_start <> ""){
 	$date_start = str_replace('-','',$date_start);
 	$cond_date_start = " and evndte >= '".$date_start."'";
}else{
 	$cond_date_start = "";
}
//สร้างเงื่อนไข วันที่สิ้นสุด
if($date_end <> ""){
 	$date_end = str_replace('-','',$date_end);
  	$cond_date_end = " and evndte <= '".$date_end."'";
}else{
 	$cond_date_end = "";
}
$sql = "select sex,mis_losbody,count(*) as c from mis_numvictim_losbody where sex in ('1','2') and mis_losbody in ('1','2') ".$cond_prov_str.$cond_date_start.$cond_date_end.$cond_cause_str." group by sex,mis_losbody order by sex,mis_losbody asc";
//echo $sql;
$rs=mysql_query($sql);
echo "<font color=blue><br><b>ผลการสอบถามในลักษณะตาราง</b><br><br>";
echo "<table width=500 border=1 cellspacing=0 align=center><tr><td><b>เพศ</b></td><td><b>การบาดเจ็บ</b></td><td><b>จำนวน(คน)</b></td></tr>";
while($row= mysql_fetch_array($rs)){
	echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>";
}
echo "</table></font>";
mysql_close($c);
}
?>
      </div></td>
</tr>
</table>
</form>
